package mycode;

import java.lang.reflect.Method;

import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JComboBoxOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JMenuItemOperator;
import org.netbeans.jemmy.operators.JMenuOperator;
import org.netbeans.jemmy.operators.JPasswordFieldOperator;
//import org.netbeans.jemmy.operators.JScrollPaneOperator;
import org.netbeans.jemmy.operators.JTableOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class DeploymentManageTest {

	private ClassReference application;

	@BeforeSuite
	public void setup() throws ClassNotFoundException {
		application = new ClassReference("itat.zttc.emp.viewer.RunFrame");
	}

	@AfterSuite
	public void teardown() {
		// /
	}

	@SuppressWarnings("unused")
	@BeforeMethod
	public void startApplication(Method method) throws Exception {
		application.startApplication();
	}

	@AfterMethod
	public void closeApplication() {
		// application.
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//
	@Test
	public void test() {
		// JFrameOperator mainFrame = new JFrameOperator("员工管理系统登录");
		JFrameOperator mainFrame = new JFrameOperator();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 输入用户名
		JTextFieldOperator jtfo0 = new JTextFieldOperator(mainFrame, 0);
		jtfo0.setText("admin");

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 输入密码
		JPasswordFieldOperator jtfo1 = new JPasswordFieldOperator(mainFrame, 0);
		jtfo1.setText("123");

		// 点击登陆
		JButtonOperator jbo = new JButtonOperator(mainFrame);
		jbo.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// ***********************************************************************************
		JFrameOperator mainFrame1 = new JFrameOperator();

		// 点击部门管理
		JMenuOperator jmo = new JMenuOperator(mainFrame1, 0);
		jmo.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// JMenuItem jmi=new JMenuItem();
		// jmo.pushMenuNoBlock("员工管理");
		// jmi.doClick();

		// jmo.pushMenu("员工管理").doClick();
		// jmo.pushMenu("部门管理");

		// ************************************************

		// 点击部门信息管理
		JMenuItemOperator jmio = new JMenuItemOperator(mainFrame1, "部门信息管理");
		jmio.push();

		// ***********************************************

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 点击 添加部门
		jbo = new JButtonOperator(mainFrame1, "添加部门");
		jbo.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// JFrameOperator mainFrame2 = new JFrameOperator();//这是一个新的Frame了

		JDialogOperator jdo = new JDialogOperator("添加部门");

		// 输入部门名称
		jtfo0 = new JTextFieldOperator(jdo);
		jtfo0.setText("Functional QA");

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 点击添加部门 ,这样部门就创建成功了

		jbo = new JButtonOperator(jdo, "添加部门");
		jbo.push();
		// // JMenuItemOperator jmio=new JMenuItemOperator(jmi);
		//
		// *****************************************************************************************************
		// 点击员工管理

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		jmo = new JMenuOperator(mainFrame1, 1); // 通过控件的序列号来查找控件
		jmo.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 点击员工信息管理

		jmio = new JMenuItemOperator(mainFrame1, "员工信息管理");
		jmio.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 选择第三个部门

		JComboBoxOperator jcbo = new JComboBoxOperator(mainFrame1);
		jcbo.setSelectedIndex(2);

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 点击筛选

		jbo = new JButtonOperator(mainFrame1, "筛选");
		jbo.push();

		//JScrollPaneOperator jscrpo = new JScrollPaneOperator(mainFrame1);
		JTableOperator jtabo = new JTableOperator(mainFrame1);
		String str = (String) jtabo.getValueAt(1, 1);
		System.out.println(str);

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 输入部门名称
		jtfo0 = new JTextFieldOperator(mainFrame1);
		jtfo0.setText(str);

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 点击筛选
		jbo = new JButtonOperator(mainFrame1, "筛选");
		jbo.push();

		// new JButtonOperator(mainFrame, "Reload insfdsf").push();
		// JTreeOperator tree = new JTreeOperator(mainFrame);
		//
		// //click in the middle of the tree
		// tree.clickMouse();
		// JTextFieldOperator testField = new JTextFieldOperator(mainFrame);
		//
		// //type new value in the text field
		// testField.clearText();
		// try {
		// Thread.sleep(4000);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// testField.typeText("3");
		// *****************************************************************************************************
		// 点击员工管理

		jmo = new JMenuOperator(mainFrame1, 1); // 通过控件的序列号来查找控件
		jmo.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 点击员工信息管理

		jmio = new JMenuItemOperator(mainFrame1, "员工信息管理");
		jmio.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 选择第三行的员工
		jtabo = new JTableOperator(mainFrame1);
		jtabo.selectCell(0, 1);

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 点击删除员工
		jbo = new JButtonOperator(mainFrame1, "删除员工");
		jbo.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 点击 确定

		JDialogOperator jcdo = new JDialogOperator();
		jbo = new JButtonOperator(jcdo, "Yes");
		jbo.push();

		// *****************************************************************************************************
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 点击部门管理
		jmo = new JMenuOperator(mainFrame1, 0);
		jmo.push();

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// JMenuItem jmi=new JMenuItem();
		// jmo.pushMenuNoBlock("员工管理");
		// jmi.doClick();

		// jmo.pushMenu("员工管理").doClick();
		// jmo.pushMenu("部门管理");

		//

		// 点击部门信息管理
		jmio = new JMenuItemOperator(mainFrame1, "部门信息管理");
		jmio.push();

		//

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 选择第三行的部门
		jtabo = new JTableOperator(mainFrame1);
		jtabo.selectCell(2, 1);

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 点击修改部门
		jbo = new JButtonOperator(mainFrame1, "修改部门");
		jbo.push();

		// 修改名字叫做MSCI

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jdo = new JDialogOperator("更新部门");
		jtfo0 = new JTextFieldOperator(jdo);
		jtfo0.setText("MSCI");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jbo = new JButtonOperator(jdo, "更新部门");
		jbo.push();
	}
}
